﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DevEx.Thanh.Class ;
using Microsoft.SqlServer.Management.Smo;

namespace DevEx.Thanh.UserControl
{
    public partial class frmLogin : DevExpress.XtraEditors.XtraForm
    {
        private CUserController ctrlUser = new CUserController();
        private string strConnect = global::DevEx.Properties.Settings.Default.QLVTConnectionString;

        public frmLogin()
        {
            InitializeComponent();
        }

        private void frmLogin_Load(object sender, EventArgs e)
        {
            //lấy thông tin server
            string serverName = Thanh.Utilities.XL_DULIEU.GetGiaTriThanhPhan("Data Source", strConnect);
            lblServer.Text = serverName;
            LoadDatabase();
            lblPasswordError.Visible = false;
            lblUserNameError.Visible = false;GlobalVar.LoginOK = false;
            GlobalVar.CurrentUser = null;
        }
        private void LoadDatabase()
        {
            try
            {
                cmbDataBase.Items.Clear();
                dynamic lst =Thanh.Utilities.XL_DULIEU.GetDatabaseList();
                foreach (string p in lst)
                {
                    cmbDataBase.Items.Add(p);
                }if (cmbDataBase.Items.Count > 0)
                {
                    string tenDB = Thanh.Utilities.XL_DULIEU.GetGiaTriThanhPhan("Initial Catalog", strConnect);
                    if (cmbDataBase.Items.Contains(tenDB))
                    {
                        cmbDataBase.Text = tenDB;
                    }else
                    {
                        cmbDataBase.Text = cmbDataBase.Items[0].ToString();
                    }
                }
               
            }
            catch (Exception ex)
            {
                throw ex;
            }

        }
        private void simpleButton1_Click(object sender, EventArgs e)
        {
            //kiểm tra
            if (String.IsNullOrEmpty(txtUsername.Text))
            {
                lblUserNameError.Visible = true;
                return;
            }
            if (String.IsNullOrEmpty(txtPassword.Text))
            {
                lblPasswordError.Visible = true;
                return;
            }
            //kiểm tra username pass
            if (ctrlUser.CheckUser(txtUsername.Text, txtPassword.Text))
            {
                GlobalVar.CurrentUser = ctrlUser.SelectUserByUserName(txtUsername.Text);
                GlobalVar.LoginOK = true;
                //kiểm tra xem kết nối có thay đổi ko
                string database = Thanh.Utilities.XL_DULIEU.GetGiaTriThanhPhan("Initial Catalog", strConnect);
                if (cmbDataBase.Text != database)
                {

                    string server = lblServer.Text; database = cmbDataBase.Text;
                    string user = Thanh.Utilities.XL_DULIEU.GetGiaTriThanhPhan("User ID", strConnect);
                    string pass = Thanh.Utilities.XL_DULIEU.GetGiaTriThanhPhan("Password", strConnect);
                    string connectString = String.Format("Data Source={0};Initial Catalog={1};Persist Security Info=True;User ID={2};Password={3}", server, cmbDataBase.Text, user, pass);

                    //string KEY_CONNECTSTRING = "DevEx.Properties.Settings.QLVTConnectionString";
                    //System.Configuration.Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
                    //config.ConnectionStrings.ConnectionStrings[KEY_CONNECTSTRING].ConnectionString = connectString;
                    //config.Save(ConfigurationSaveMode.Full);
                    ////Load lại Config
                    //ConfigurationManager.RefreshSection("connectionStrings");

                    global::DevEx.Properties.Settings.Default.QLVTConnectionString = connectString;
                    strConnect = connectString;}
                GlobalVar.FormMain.Text = "Nhà máy A41 - Chương trình quản lý vật tư - Cơ sở dữ liệu: " + cmbDataBase.Text;
                this.Close();
            }
            else
            {
                GlobalVar.LoginOK = false;
                GlobalVar.CurrentUser = null;
                if (MessageBox.Show( "Tên đăng nhập hoặc mật khẩu không đúng. Đăng nhập lại?","Thông báo", MessageBoxButtons.OKCancel )==DialogResult.OK )
                {
                    lblPasswordError.Visible = false;
                    lblUserNameError.Visible = false;
                    txtUsername.EditValue = "";
                    txtPassword.EditValue = "";
                    txtUsername.Focus();
                }
                else
                {
                    this.Close();
                }
            }
        }

        private void btHuy_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}